//
//  HomeTableViewController.swift
//  weibo
//
//  Created by 陈伟 on 15/10/14.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import UIKit

let NormalStatusCellID = "NormalStatusCell"
let RetweedStatusCellID = "RetweedStatusCell"

class HomeTableViewController: VisitorTableViewController {


    private let statusListModel = StatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        if !UserAccountViewModel.sharedUserAccount.userLogin{
        visitorView?.setupInfo(nil , title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
            return
        }
        
        prepareTabelView()
        loadData()
    }
    
    //设置CELL的各种参数
    private func prepareTabelView() {
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: RetweedStatusCellID)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: NormalStatusCellID)
        tableView.separatorStyle = .None
        //预估行高
//        tableView.estimatedRowHeight = 100
//        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 400
//        tableView.rowHeight = 400
        refreshControl = WBRefreshControl()
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl?.tintColor = UIColor.clearColor()
        
        //底部视图  上拉刷新
        tableView.tableFooterView = pullupView
        
    }
    //加载数据
    @objc private func loadData(){
        
        refreshControl?.beginRefreshing()

        statusListModel .loadStatusData(ispullup: pullupView.isAnimating()){ (isSuccess) -> () in
            //停止尾部动画
            self.pullupView.stopAnimating()
            //停止头部动画
            self.refreshControl?.endRefreshing()
            if !isSuccess {
                print("加载失败  table")
            }else{
                self.tableView.reloadData()
            }
//        NetworkTools.sharedTools.loadStatus { (result, error) -> () in
//            if error != nil {
//                print("数据加载失败，请重试")
//                return
//            }
////            print(result)
//            
//            guard let statusData = result?["statuses"] as?  [[String: AnyObject]] else {
//                print("数据错误")
//                return
//            }
////            print(statusData)
//            
//            var tempArray = [Status]()
//
//            for dict in statusData {
//               let statusModel = Status.init(dict: dict)
//                tempArray.append(statusModel)
//            }
//            self.dataList = tempArray
//            self.tableView.reloadData()
        }
    }
    //上拉刷新数据
    private lazy var pullupView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicator.color = UIColor.redColor()
        return indicator
    }()
}

extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListModel.dataList.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let vm = statusListModel.dataList[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(vm.cellId, forIndexPath: indexPath)  as! StatusCell
        cell.statusModel  = vm
        
        //如果是最后一条微博， 刷新
        if indexPath.row == statusListModel.dataList.count - 1 && !pullupView.isAnimating() {
            pullupView.startAnimating()
            loadData()
        }
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let vm = statusListModel.dataList[indexPath.row]
//        let cell = StatusCell(style: UITableViewCellStyle.Default
//            , reuseIdentifier: HomeTBStatusCell)
        return vm.rowHeight
    }
}
